package com.atguigu.gmall.product.Rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.eneity.SkuImage;
import com.atguigu.gmall.product.eneity.SkuInfo;
import com.atguigu.gmall.product.eneity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/rpc/product")
public class SkuDetailRpcController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    /**
     * 获取商品基本信息
     * @return
     */
    @GetMapping("/skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable Long skuId){

        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 获取商品图片
     * @param skuId
     * @return
     */
    @GetMapping("/skuImages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable Long skuId){

        List<SkuImage> skuImages = skuImageService.lambdaQuery().eq(SkuImage::getSkuId, skuId).list();
        return Result.ok(skuImages);
    }

    /**
     * 获取商品实时价格
     * @param skuId
     * @return
     */
    @GetMapping("/skuPrice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable Long skuId){

        SkuInfo one = skuInfoService.lambdaQuery().select(SkuInfo::getPrice).eq(SkuInfo::getId, skuId).one();

        return Result.ok(one.getPrice());
    }

    /**
     * 获取商品销售属性
     * @param spuId
     * @return
     */
    @GetMapping("/skuSalAttr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable Long spuId ,@PathVariable Long skuId){

        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrANdValueAndOrder(spuId,skuId);
        return Result.ok(list);
    }

    /**
     * 获取ValuesJson
     * @param spuId
     * @return
     */
    @GetMapping("ValuesJson/{spuId}")
    public Result<String> getValuesJson(@PathVariable Long spuId){

        String json = spuSaleAttrService.getValueJson(spuId);
        return Result.ok(json);
    }




}
